package com.reform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.reform.pojo.project.ProjectSearching;
import com.reform.mapper.ProjectSearchingMapper;
import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.IProjectSearchingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reform.utils.VerifycUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 主要科学研究和教学研究工作简历
 * 服务实现类
 * </p>
 *
 * @author mortale
 * @since 2021-07-20
 */
@Service
public class ProjectSearchingServiceImpl extends ServiceImpl<ProjectSearchingMapper, ProjectSearching> implements IProjectSearchingService {

    @Autowired
    ProjectSearchingMapper projectSearchingMapper;

    @Override
    public Integer creatProjectSearchingList(List<ProjectSearching> projectSearchingList) throws SQLException {
        int res = 0;
        for (ProjectSearching projectSearching : projectSearchingList) {
            res += projectSearchingMapper.insert(projectSearching);
        }
        if (res != projectSearchingList.size())
            return -1;
        return res;
    }

    @Override
    public Integer deleteProjectSearchingByProId(String proId) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        map.put("pro_id", proId);
        return projectSearchingMapper.deleteByMap(map);
    }

    @Override
    public Integer updateProjectSearchingList(List<ProjectSearching> projectSearchingList) throws SQLException {
        int res = 0;
        for (ProjectSearching projectSearching : projectSearchingList) {
            projectSearchingMapper.updateById(projectSearching);
            res++;
        }
        return res;
    }

    @Override
    public Result deleteProjectSearchingBySearchingId(String searching_id) throws SQLException {
        if (projectSearchingMapper.deleteById(searching_id) > 0)
            return Result.success(ResultCode.SUCCESS);
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public Result insertProjectSearching(String projectSearchings) throws SQLException {
        List<ProjectSearching> projectSearchingList;
        try {
            projectSearchingList = JSON.parseArray(projectSearchings, ProjectSearching.class);
        } catch (JSONException e) {
            return Result.failure(ResultCode.PARAM_JSON_IS_WRONG, e.getMessage());
        }
        int res = 0;
        for (ProjectSearching projectSearching : projectSearchingList) {
            if (!VerifycUtils.verifyNullOrEmptyUtils(projectSearching.getProId())) {
                return Result.failure(ResultCode.PARAM_IS_MISSING);
            }
        }
        for (ProjectSearching projectSearching : projectSearchingList) {
            res += projectSearchingMapper.insert(projectSearching);
        }
        if (res != projectSearchingList.size()) {
            return Result.failure(ResultCode.FAILURE);
        }
        return Result.success(ResultCode.SUCCESS);
    }
}
